package com.hr.service.impl;

import com.hr.mapper.*;
import com.hr.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.org.mozilla.javascript.internal.EcmaError;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息操作业务实现层
 */
@Service
@Transactional
@SuppressWarnings("ALL")
public class UserServiceImpl implements UserService {

    @Autowired
    private HrUserInfoMapper hrUserInfoMapper ;

    @Override
    public int countByExample(HrUserInfoExample example) {
        return hrUserInfoMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(HrUserInfoExample example) {
        return hrUserInfoMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer userid) {
        return hrUserInfoMapper.deleteByPrimaryKey(userid);
    }

    @Override
    public int insert(HrUserInfo record) {
        return hrUserInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(HrUserInfo record) {
        return hrUserInfoMapper.insertSelective(record);
    }

    @Override
    public List<HrUserInfo> selectByExample(HrUserInfoExample example) {
        return hrUserInfoMapper.selectByExample(example);
    }

    @Override
    public HrUserInfo selectByPrimaryKey(Integer userid) {
        return hrUserInfoMapper.selectByPrimaryKey(userid);
    }

    @Override
    public int updateByExampleSelective(HrUserInfo record, HrUserInfoExample example) {
        return hrUserInfoMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(HrUserInfo record, HrUserInfoExample example) {
        return hrUserInfoMapper.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(HrUserInfo record) {
        return hrUserInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(HrUserInfo record) {
        return hrUserInfoMapper.updateByPrimaryKey(record);
    }

    @Autowired
    private HrUserdeptstationMapper hrUserdeptstationMapper;
    @Autowired
    private HrDeptMapper hrDeptMapper;
    @Autowired
    private HrStationMapper hrStationMapper;
    @Autowired
    private HrSalarytypeMapper hrSalarytypeMapper;
    /**
     * @Author: WuJiwei
     * @Date: 2017/12/20 23:26
     * @Description: 查询所有用户
     * @Params: [hrUserInfos]
     * @Return java.util.List<com.hr.pojo.UserInfos>
     */
    @Override
    public List<UserInfos> findAllUserInfo(List<HrUserInfo> hrUserInfos) {
        UserInfos userInfos = new UserInfos();
        List<UserInfos> result = new ArrayList<UserInfos>();
        HrUserdeptstation hrUserdeptstation = null;
        HrDept hrDept = null;
        HrStation hrStation = null;
        HrSalarytype hrSalarytype = null;
        HrUserdeptstationExample hrUserdeptstationExample = new HrUserdeptstationExample();
        HrUserdeptstationExample.Criteria criteria1 = hrUserdeptstationExample.createCriteria();
        HrDeptExample hrDeptExample = new HrDeptExample();
        HrDeptExample.Criteria criteria2 = hrDeptExample.createCriteria();
        HrStationExample hrStationExample = new HrStationExample();
        HrStationExample.Criteria criteria3 = hrStationExample.createCriteria();
        HrSalarytypeExample hrSalarytypeExample = new HrSalarytypeExample();
        HrSalarytypeExample.Criteria criteria4 = hrSalarytypeExample.createCriteria();

        for (HrUserInfo user:hrUserInfos) {
            criteria1.andUseridEqualTo(user.getUserid());
            hrUserdeptstation = hrUserdeptstationMapper.selectByExample(hrUserdeptstationExample).get(0);
            
            criteria2.andDeptidEqualTo(hrUserdeptstation.getDeptid());
            hrDept = hrDeptMapper.selectByExample(hrDeptExample).get(0);

            criteria3.andStationidEqualTo(hrUserdeptstation.getStationid());
            hrStation = hrStationMapper.selectByExample(hrStationExample).get(0);

            criteria4.andSalarytypeEqualTo(user.getSalarylevel());
            hrSalarytype = hrSalarytypeMapper.selectByExample(hrSalarytypeExample).get(0);

            userInfos.setHrUserInfo(user);
            userInfos.setHrDept(hrDept);
            userInfos.setHrStation(hrStation);
            userInfos.setHrSalarytype(hrSalarytype);
            result.add(userInfos);
        }
        return result;
    }

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/19 22:52
     * @Description: 根据用户id 删除用户信息, 在真正开发中是不删除用户信息的,而是改变用户状态.
     * @Params: [userId]
     * @Return com.hr.pojo.ResultInfo
     */
    @Override
    public ResultInfo delectUserInfo(Integer userId) {
        ResultInfo resultInfo = new ResultInfo();
        int userid1 = userId==null ? 0:userId;
        HrUserInfo hrUserInfo = hrUserInfoMapper.selectByPrimaryKey(userid1);
        if(hrUserInfo==null){
            return new ResultInfo(-1,"该用户不存在!");
        }
        HrUserdeptstationExample hrUserdeptstationExample = new HrUserdeptstationExample();
        HrUserdeptstationExample.Criteria criteria = hrUserdeptstationExample.createCriteria();
        criteria.andUseridEqualTo(userid1);
        try {
            HrUserdeptstation hrUserdeptstation = hrUserdeptstationMapper.selectByExample(hrUserdeptstationExample).get(0);
            hrDeptMapper.deleteByPrimaryKey(hrUserdeptstation.getDeptid());
            hrStationMapper.deleteByPrimaryKey(hrUserdeptstation.getStationid());
            hrUserdeptstationMapper.deleteByExample(hrUserdeptstationExample);
            resultInfo.setRet(1);
            resultInfo.setMsg("用户删除成功!");
        }catch (Exception e){
            e.printStackTrace();
            resultInfo.setRet(-1);
            resultInfo.setMsg("删除用户失败!");
        }
        return resultInfo;
    }

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/20 23:30
     * @Description: 添加用户
     * @Params: [userInfos]
     * @Return com.hr.pojo.ResultInfo
     */
    @Override
    public ResultInfo addUserInfo(UserInfos userInfos) {
        ResultInfo resultInfo = new ResultInfo();
        try {
            int userId = hrUserInfoMapper.insert(userInfos.getHrUserInfo());
            int deptId = hrDeptMapper.insert(userInfos.getHrDept());
            int stationId = hrStationMapper.insert(userInfos.getHrStation());

            HrUserdeptstation hrUserdeptstation = new HrUserdeptstation();
            hrUserdeptstation.setDeptid(deptId);
            hrUserdeptstation.setUserid(userId);
            hrUserdeptstation.setStationid(stationId);
            hrUserdeptstationMapper.insert(hrUserdeptstation);

            resultInfo.setRet(1);
            resultInfo.setMsg("添加用户成功!");
        }catch (Exception e){
            e.printStackTrace();
            resultInfo.setRet(-1);
            resultInfo.setMsg("添加用户失败!");
        }
        return resultInfo;
    }

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/20 23:32
     * @Description: 编辑用户信息
     * @Params: [userInfos]
     * @Return com.hr.pojo.ResultInfo
     */
    @Override
    public ResultInfo editUserInfo(UserInfos userInfos) {
        ResultInfo resultInfo = new ResultInfo();
        try {
            hrUserInfoMapper.updateByPrimaryKey(userInfos.getHrUserInfo());

            HrUserdeptstationExample hrUserdeptstationExample = new HrUserdeptstationExample();
            HrUserdeptstationExample.Criteria criteria = hrUserdeptstationExample.createCriteria();
            criteria.andUseridEqualTo(userInfos.getHrUserInfo().getUserid());

            HrUserdeptstation hrUserdeptstation = new HrUserdeptstation();
            hrUserdeptstation.setStationid(userInfos.getHrStation().getStationid());
            hrUserdeptstation.setUserid(userInfos.getHrUserInfo().getUserid());
            hrUserdeptstation.setDeptid(userInfos.getHrDept().getDeptid());

            hrUserdeptstationMapper.updateByExample(hrUserdeptstation,hrUserdeptstationExample);
        }catch (Exception e){
            e.printStackTrace();
            resultInfo.setRet(-1);
            resultInfo.setMsg("用户信息编辑失败!");
        }
        return resultInfo;
    }


}
